//
//  XTCommon.m
//  XTMuteOpen
//
//  Created by bushAK on 2024/1/22.
//

#import "XTCommon.h"
#import "XTMuteOpen-Swift.h"
#import <SocketIO-Swift.h>
#import <WebRTC/WebRTC.h>

@implementation XTCommon

/// 底部安全区高度
+ (CGFloat)getSafePaddingBottom {
    if (@available(iOS 13.0, *)) {
        NSSet *set = [UIApplication sharedApplication].connectedScenes;
        UIWindowScene *windowScene = [set anyObject];
        UIWindow *window = windowScene.windows.firstObject;
        return window.safeAreaInsets.bottom;
    } else if (@available(iOS 11.0, *)) {
        UIWindow *window = [UIApplication sharedApplication].windows.firstObject;
        return window.safeAreaInsets.bottom;
    }
    return 0;
}

+(CGFloat)getSafePaddingTop {
    if (@available(iOS 13.0, *)) {
        NSSet *set = [UIApplication sharedApplication].connectedScenes;
        UIWindowScene *windowScene = [set anyObject];
        UIWindow *window = windowScene.windows.firstObject;
        return window.safeAreaInsets.top;
    } else if (@available(iOS 11.0, *)) {
        UIWindow *window = [UIApplication sharedApplication].windows.firstObject;
        return window.safeAreaInsets.top;
    }
    return 0;
}

+(void)saveUser:(XTUserModel *)model{
    NSData *archivedData =  [NSKeyedArchiver archivedDataWithRootObject:model requiringSecureCoding:YES error:nil];
    NSString *filePath = [self getFilePath];

    [NSKeyedArchiver archiveRootObject:model toFile:filePath];
    [archivedData writeToFile:filePath atomically:YES];
}
+(XTUserModel *)getUser{
    NSString *filePath = [self getFilePath];
    XTUserModel *data = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    if (data) {
         return data;
    }else{
        return [[XTUserModel alloc]init];;
    }
}

+(void)removeUserFromLocoal{
    XTUserModel * model = [XTUserModel new];
    [self saveUser:model];
}
+(BOOL)isLogin{
    XTUserModel * model = [self getUser];
    return  model.token.length > 5;
}
+(NSString * )getToken{
    XTUserModel * model = [self getUser];
    return  model.token;
}

+(void)initWebRtc;{
    NSDictionary *fieldTrials = @{};
    RTCInitFieldTrialDictionary(fieldTrials);
    RTCInitializeSSL();
    RTCSetupInternalTracer();
    [RTCPeerConnectionFactory initialize];
}
+(void)cleanupSSL{
    RTCShutdownInternalTracer();
    RTCCleanupSSL();
}
 
+ (NSString *)getFilePath  {
    NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = [documentDirectories firstObject];
    NSString *path = [documentDirectory stringByAppendingPathComponent: @"user_archieve.data"];
     return path;
}




+(NSString *)get_api_base;{
    return @"https://api.kyyai.com";
}
+(NSString *)get_api_sms_code;{
    return [NSString stringWithFormat:@"%@%@",[self get_api_base],@"/sms/send_auth_code"];
}

+(NSString *)get_api_pwd_login;{
    return [NSString stringWithFormat:@"%@%@",[self get_api_base],@"/api/password/login"];
}

+(NSString *)get_api_code_login;{
    return [NSString stringWithFormat:@"%@%@",[self get_api_base],@"/api/authcode/login"];
}

+(NSString *)get_api_list_vms;{
    return [NSString stringWithFormat:@"%@%@",[self get_api_base],@"/api/list/vms"];
}
+(NSString *)get_api_get_turns;{
    return [NSString stringWithFormat:@"%@%@",[self get_api_base],@"/urls/turnservers"];
}

+(NSString *)get_api_get_rtc_url;{
    return @"https://webrtc.kyyai.com";
}

 

@end
